<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="18" :xs="24">
        <el-card>
          <ListConfession />
        </el-card>
      </el-col>
      <el-col :span="6" :xs="24">
        <Description class="user-card" :user="user" style="position: fixed;margin-right: 20px" />
      </el-col>
    </el-row>
    <div
      class="el-backtop"
      style="right: 40px; bottom: 40px;"
      @click="scrollTop"
    >
      <i class="el-icon-caret-top" />
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import Description from './components/Description.vue'
import ListConfession from './components/ConfessionList.vue'

export default {
  name: 'Profile',
  components: { Description, ListConfession },
  data() {
    return {
      user: {},
      headerHeight: 86
    }
  },
  computed: {
    ...mapGetters([
      'name',
      'avatar',
      'roles'
    ])
  },
  mounted() {
    this.addScrollListener()
  },
  beforeDestroy() {
    this.removeScrollListener()
  },
  created() {
    this.getUser()
  },
  methods: {
    scrollTop() {
      window.scrollTo(0, 0)
    },
    getUser() {
      this.user = {
        name: this.name,
        role: this.roles.join(' | '),
        email: 'admin@test.com',
        avatar: this.avatar
      }
    },
    addScrollListener() {
      window.addEventListener('scroll', this.handleScroll)
    },
    removeScrollListener() {
      window.removeEventListener('scroll', this.handleScroll)
    },
    handleScroll() {
      const scrollTop = document.documentElement.scrollTop
      const newTop = Math.max(0, this.headerHeight - scrollTop)
      const usercard = document.querySelector('.user-card')
      usercard.style.top = newTop + 'px'
    }
  }
}
</script>
<style lang="scss" scoped>
.app-container {
  padding: 0;
}
</style>
